package com.tiny.persistence.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.log4j.Logger;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.tiny.persistence.domain.CommentInstallTrain;
import com.tiny.persistence.domain.Repair;
/**
 * 设备安装及培训评价仓储层
* @ClassName:  
* @Description: TODO 
* @author:  
* @date: 2014年3月23日
*
 */
public class CommentInstallTrainRepoImpl extends BaseRepoImpl<CommentInstallTrain> {

    private static Logger logger = Logger.getLogger(CommentInstallTrainRepoImpl.class);

    @Override
    public List<CommentInstallTrain> getListByCondition(Map<String, Object> condition) {
        StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("from CommentInstallTrain as a where 1=1 ");
        
        //查询条件
        String comment = null;
        if(condition.get("commnetedContent")!=null){
            comment = String.valueOf(condition.get("commnetedContent"));
        }
        if(comment!=null){
            sqlquery.append(" and a.comment like :comment"); 
        }

        Query query = em.createQuery(sqlquery.toString());
        if(comment!=null){
            query.setParameter("comment", "%"+comment+"%");
        }
        
        UserContext uc = (UserContext) condition.get(Constants.USER_CONTEXT);
        query.setFirstResult((Integer) condition.get(Constants.KEY_RECORD_FROM));
        query.setMaxResults(uc.getPageSize());
        List<CommentInstallTrain> list = query.getResultList();
        
        logger.info("======= " + list);
        
        return list;
    }

    @Override
    public int getCountByCondition(Map<String, Object> condition) {
        StringBuffer sqlquery = new StringBuffer();
        sqlquery.append("select count(*) from CommentInstallTrain as a where 1=1 ");
        
        //查询条件
        String comment = null;
        if(condition.get("commnetedContent")!=null){
            comment = String.valueOf(condition.get("commnetedContent"));
        }
        if(comment!=null){
            sqlquery.append(" and a.comment like :comment"); 
        }

        Query query = em.createQuery(sqlquery.toString());
        if(comment!=null){
            query.setParameter("comment", "%"+comment+"%");
        }
        Object obj = query.getSingleResult();
        logger.info("======= count=" + obj);        
        return Integer.parseInt(obj.toString());
    }
    
    
}
